﻿using System.Data.SqlClient;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;

namespace Tisbi.LawFirm.UI.ViewModel
{
    public class SqlConnectionStringViewModel : ViewModelBase
    {
        private readonly SqlConnectionStringBuilder _builder = new SqlConnectionStringBuilder
        {
            Pooling = false,
            IntegratedSecurity = true,
            ApplicationName = "EntityFramework"
        };

        [PreferredConstructor]
        public SqlConnectionStringViewModel()
        {
        }

        public SqlConnectionStringViewModel(string connectionString)
        {
            _builder.ConnectionString = connectionString;
        }

        public static implicit operator string(SqlConnectionStringViewModel connectionString)
        {
            return connectionString.ToString();
        }

        public override string ToString()
        {
            if (Server.EndsWith(".sdf"))
                if (string.IsNullOrEmpty(Password))
                    return new SqlConnectionStringBuilder {DataSource = Server}.ConnectionString;
                else
                    return new SqlConnectionStringBuilder {DataSource = Server, Password = Password}.
                        ConnectionString;

            return _builder.ConnectionString;
        }

        /// <summary>
        /// Creates a copy of this connection string with the specified database instead of the current
        /// </summary>
        /// <param name="databaseName">Name of the database.</param>
        /// <returns></returns>
        public SqlConnectionStringViewModel WithDatabase(string databaseName)
        {
            return new SqlConnectionStringViewModel
            {
                Server = Server,
                Database = databaseName,
                IntegratedSecurity = IntegratedSecurity,
                UserName = UserName,
                Password = Password,
                Pooling = Pooling
            };
        }

        public string Server
        {
            get { return _builder.DataSource; }
            set
            {
                if (_builder.DataSource == value)
                {
                    return;
                }
                _builder.DataSource = value;
                RaisePropertyChanged(() => Server);
            }
        }

        public string Database
        {
            get { return _builder.InitialCatalog; }
            set
            {
                if (_builder.InitialCatalog == value)
                {
                    return;
                }
                _builder.InitialCatalog = value;
                RaisePropertyChanged(() => Database);
            }
        }

        public string UserName
        {
            get { return _builder.UserID; }
            set
            {
                if (_builder.UserID == value)
                {
                    return;
                }
                _builder.UserID = value;

                RaisePropertyChanged(() => UserName);
            }
        }

        public bool Pooling
        {
            get { return _builder.Pooling; }
            set
            {
                if (_builder.Pooling == value)
                {
                    return;
                }
                _builder.Pooling = value;
                RaisePropertyChanged(() => Pooling);
            }
        }

        public int ConnectionTimeout
        {
            get { return _builder.ConnectTimeout; }
            set
            {
                if (_builder.ConnectTimeout == value)
                {
                    return;
                }
                _builder.ConnectTimeout = value;
                RaisePropertyChanged(() => ConnectionTimeout);
            }
        }

        public string Password
        {
            get { return _builder.Password; }
            set
            {
                if (_builder.Password == value)
                {
                    return;
                }
                _builder.Password = value;
                RaisePropertyChanged(() => Password);
            }
        }

        public bool IntegratedSecurity
        {
            get { return _builder.IntegratedSecurity; }
            set
            {
                if (_builder.IntegratedSecurity == value)
                {
                    return;
                }
                _builder.IntegratedSecurity = value;
                RaisePropertyChanged(() => IntegratedSecurity);
            }
        }

        public bool IsValid()
        {
            return
                (!string.IsNullOrEmpty(Server) && Server.EndsWith(".sdf")) ||
                (!string.IsNullOrEmpty(Server) &&
                 !string.IsNullOrEmpty(Database) &&
                 (IntegratedSecurity || (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password))));
        }

    }
}
